﻿using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using SharePointUtilitiesForDevelopers.Mapper;
using SharePointUtilitiesForDevelopers.Service;
using SharePointUtilitiesForDevelopers.ViewsService;

namespace SharePointUtilitiesForDevelopers.Gateway
{
    /// <summary>
    /// Provides a gateway to the SharePoint views service.
    /// </summary>
    public class SharePointViewsServiceGateway: ISharePointViewsServiceGateway
    {
        #region Fields

        private readonly IMapper<XElement, SharePointListView> _viewMapper;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SharePointViewsServiceGateway"/> class.
        /// </summary>
        /// <param name="viewMapper">The view mapper.</param>
        public SharePointViewsServiceGateway(IMapper<XElement, SharePointListView> viewMapper)
        {
            _viewMapper = viewMapper;
        }

        #endregion Constructors

        #region Methods

        /// <summary>
        /// Gets list views.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        /// <param name="listName">Name of the list.</param>
        /// <returns>A list of views.</returns>
        public IEnumerable<SharePointListView> GetListViews(
            string siteUrl,
            string listName)
        {
            // Get the proxy
            var client = SharePointServiceClientFactory.CreateInstance<ViewsSoapClient, ViewsSoap>(siteUrl);

            // Get the response
            var response = XElement.Parse(client.GetViewCollection(listName).OuterXml);

            // Get the mapped object
            var views = _viewMapper.MapList(response);

            // Set values not in the xml response
            views.ToList().ForEach(view => view.ListName = listName);

            // Return the mapped object
            return views;
        }

        /// <summary>
        /// Gets a list view.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        /// <param name="listName">Name of the list.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <returns>A view.</returns>
        public SharePointListView GetListView(
            string siteUrl,
            string listName,
            string viewName)
        {
            // Get the proxy
            var client = SharePointServiceClientFactory.CreateInstance<ViewsSoapClient, ViewsSoap>(siteUrl);

            // Get the response
            var response = XElement.Parse(client.GetView(listName, viewName).OuterXml);

            // Get the mapped object
            var view = _viewMapper.Map(response);

            // Set values not in the xml response
            view.ListName = listName;

            // Return the mapped object
            return view;
        }

        #endregion Methods
    }
}
